<?php

//用于创建mysql连接池
namespace Src;

class MysqlLib {
	private $config;
	private $dbpool = array();
	public $poolsize;

	public function __construct(array $config){
		$this->config = $config;
		$this->poolsize = $this->config['poolsize'];

		//准备连接池
		for($index = 1; $index <= $this->poolsize; $index++){
			$conn = mysqli_connect($this->config['host'], $this->config['user'], $this->config['password'], $this->config['db']) or die('db connect error');
			array_push($this->dbpool, $conn);
		}
	}

	//提取一条数据库链接资源
	public function get(){
		if(count($this->dbpool) <= 0){
			return false;
		}else{
			return array_pop($this->dbpool);
		}
	}

	//返还数据库链接资源
	public function release($conn){
		if(count($this->dbpool) >= $this->poolsize){
			return false;
		}else{
			array_push($this->dbpool, $conn);
		}
	}

	//查询
	public function query($mysqli,$sql){
		$data = array();
		$res = $mysqli->query($sql);
		if(is_bool($res)){
			return $res;
		}
		while($row = mysqli_fetch_assoc($res)){
			array_push($data,$row);
		}
		return $data;
	}

}